﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FlowCard
{
    public partial class frmSNQuery : Form
    {
        SQLTools sqt;

        public frmSNQuery()
        {
            InitializeComponent();
        }

        private void frmSNQuery_Load(object sender, EventArgs e)
        {
            sqt = new SQLTools();
            iniComboBox();
            rbtnACard_CheckedChanged(this, new EventArgs());
            timer1.Enabled = true;
        }

        private void rbtnACard_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnACard.Checked)
            {
                rbtnBCard.Checked = false;
                rbtnCCard.Checked = false;
                Query1();
            }
        }

        private void rbtnBCard_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnBCard.Checked)
            {
                rbtnACard.Checked = false;
                rbtnCCard.Checked = false;
                Query1();
            }
        }

        private void rbtnCCard_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnCCard.Checked)
            {
                rbtnACard.Checked = false;
                rbtnBCard.Checked = false;
                Query1();
            }
        }

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!cmbType.Text.Equals(string.Empty))
            {
                if (cmbType.Text.Equals("CC片式"))
                {
                    label7.Visible = true;
                    txtChipNo.Visible = true;
                    btnQuery.Visible = true;
                }
                else
                {
                    label7.Visible = false ;
                    txtChipNo.Visible = false ;
                    btnQuery.Visible = false ;
                    Query2();
                }
            }
        }

        private void Query1()
        {
            string strCardType = string.Empty;
            if (rbtnACard.Checked)
            {
                strCardType = "A";
            }
            if (rbtnBCard.Checked)
            {
                strCardType = "B";
            }
            if (rbtnCCard.Checked)
            {
                strCardType = "C";
            }
            sqt.FillComboBox(cmbType, "select AssignType 分配类型 from SNAssign where CardType='" + strCardType + "' order by AssignType", true);
            cmbType.Items.Add("CC片式");
            if (cmbType.Items.Count > 0)
            {
                cmbType.SelectedIndex = 0;
            }
            else
            {
                MessageBox.Show("没有设置该类型卡的卡号分配方案！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Query2()
        {
            string strCardType = string.Empty;
            if (rbtnACard.Checked)
            {
                strCardType = "A";
            }
            if (rbtnBCard.Checked)
            {
                strCardType = "B";
            }
            if (rbtnCCard.Checked)
            {
                strCardType = "C";
            }
            string strTmpYear = cmbYear.Text;
            string strTmpMonth = cmbMonth.Text;
            string strTmp = string.Empty;
            if (strCardType.Equals("A"))
            {
                string[] strY = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K" };
                strTmp = strTmpYear.Substring(2, 1) +                   //1
                    strY[Convert.ToInt32(strTmpYear.Substring(3, 1))] +  //F
                    strTmpMonth;
            }
            else
            {
                strTmp = strTmpYear.Substring(2, 2) +                   //1
                    strTmpMonth;
            }
            DataTable tmpdt = sqt.GetDataTable("select SNStart 卡号起,SNEnd 卡号止,Memo 备注 from SNAssign where CardType='" + strCardType + "' and AssignType='" + cmbType.Text + "'");
            txtSNStart.Clear();
            txtSNEnd.Clear();
            txtMemo.Clear();
            if (tmpdt.Rows.Count > 0)
            {
                int iSNStart = Convert.ToInt32(tmpdt.Rows[0]["卡号起"]);
                int iSNEnd = Convert.ToInt32(tmpdt.Rows[0]["卡号止"]);
                txtSNStart.Text = strTmp + iSNStart.ToString("0000");
                txtSNEnd.Text = strTmp + iSNEnd.ToString("0000");
                txtMemo.Text = tmpdt.Rows[0]["备注"].ToString();
                DataTable tmpdt2 = new DataTable();
                tmpdt2.Columns.Add("1");
                tmpdt2.Columns.Add("2");
                tmpdt2.Columns.Add("3");
                tmpdt2.Columns.Add("4");
                tmpdt2.Columns.Add("5");
                tmpdt2.Columns.Add("6");
                tmpdt2.Columns.Add("7");
                tmpdt2.Columns.Add("8");
                tmpdt2.Columns.Add("9");
                tmpdt2.Columns.Add("10");
                int idx = iSNStart;
                int iColIdx = 0;
                DataRow dr = tmpdt2.NewRow();
                while (idx <= iSNEnd)
                {
                    if (iColIdx == 0)
                    {
                        dr = tmpdt2.NewRow();
                    }
                    dr[iColIdx] = strTmp + idx.ToString("0000");
                    idx++;
                    iColIdx++;
                    if (iColIdx == 10)
                    {
                        iColIdx = 0;
                        tmpdt2.Rows.Add(dr);
                    }
                }
                dgv2.DataSource = tmpdt2;
                dgv2.AutoResizeColumns();
                dgv2.CurrentCell = null;
                if (strCardType.Equals("B"))
                {
                    tmpdt = sqt.GetDataTable("select gd034 流转卡号,gd001 工单号,gd004 开卡人,gd020 审核否 from gd_table where (gd034 between '" + txtSNStart.Text + "0' and '" + txtSNEnd.Text + "0' or gd034 between '"+txtSNStart.Text + "' and '" + txtSNEnd.Text + "')");
                }
                else
                {
                    tmpdt = sqt.GetDataTable("select gd034 流转卡号,gd001 工单号,gd004 开卡人,gd020 审核否 from gd_table where gd034 between '" + txtSNStart.Text + "' and '" + txtSNEnd.Text + "'");
                }
                if (tmpdt.Rows.Count > 0)
                {
                    for (int i = 0; i < tmpdt.Rows.Count; i++)
                    {
                        bool bSNExists = false;
                        string strTmpSN = GetString(tmpdt.Rows[i]["流转卡号"]);
                        bool bCCSN = false;
                        if (strTmpSN.Length == 9)
                        {
                            bCCSN = true;
                            strTmpSN = strTmpSN.Substring(0, 8);
                        }
                        for (int k = 0; k < dgv2.Rows.Count; k++)
                        {
                            for (int j = 0; j < dgv2.Columns.Count; j++)
                            {
                                if (dgv2.Rows[k].Cells[j].Value.Equals(strTmpSN ))
                                {
                                    dgv2.Rows[k].Cells[j].ToolTipText = tmpdt.Rows[i]["工单号"].ToString() + "\t" + tmpdt.Rows[i]["开卡人"].ToString();
                                    if (tmpdt.Rows[i]["审核否"].Equals("Y"))
                                    {
                                        if (bCCSN)
                                        {
                                            dgv2.Rows[k].Cells[j].Style.BackColor = Color.SpringGreen;
                                        }
                                        else
                                        {
                                            dgv2.Rows[k].Cells[j].Style.BackColor = Color.Red;
                                        }
                                    }
                                    else
                                    {
                                        if (bCCSN)
                                        {
                                            dgv2.Rows[k].Cells[j].Style.BackColor = Color.Cyan;
                                        }
                                        else
                                        {
                                            dgv2.Rows[k].Cells[j].Style.BackColor = Color.Yellow;
                                        }
                                    }
                                    bSNExists = true;
                                    break;
                                }
                            }
                            if (bSNExists)
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }

        private void Query3()
        {
            if (txtChipNo.Text.Trim().Equals(string.Empty)||txtChipNo .Text .Trim ().Length !=8)
            {
                MessageBox.Show("未填写芯片号或芯片号格式不正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtChipNo.Focus();
                return;
            }
            string strCardType = string.Empty;
            strCardType = "B";
            txtSNStart.Clear();
            txtSNEnd.Clear();
            txtMemo.Clear();
            DataTable tmpdt = new DataTable();
            tmpdt.Columns.Add("1");
            tmpdt.Columns.Add("2");
            tmpdt.Columns.Add("3");
            tmpdt.Columns.Add("4");
            tmpdt.Columns.Add("5");
            tmpdt.Columns.Add("6");
            tmpdt.Columns.Add("7");
            tmpdt.Columns.Add("8");
            tmpdt.Columns.Add("9");
            tmpdt.Columns.Add("10");
            char cTmp='A';
            for(int i=0;i<26;i++)
            {
                DataRow dr=tmpdt.NewRow ();
                for(int k=0;k<tmpdt.Columns.Count ;k++)
                {
                    dr[k]=txtChipNo .Text .Trim ()+cTmp.ToString ()+k.ToString ();
                }
                tmpdt.Rows.Add (dr);
                cTmp =(char)((int)cTmp +1);
            }
            dgv2.DataSource = tmpdt;
            dgv2.AutoResizeColumns();
            DataTable tmpdt2 = sqt.GetDataTable("select gd034 流转卡号,gd001 工单号,gd004 开卡人,gd020 审核否 from gd_table where gd034 like '" + txtChipNo .Text .Trim () + "__' and gd025='B'");
            if (tmpdt2.Rows.Count > 0)
            {
                for (int i = 0; i < tmpdt2.Rows.Count; i++)
                {
                    bool bSNExists = false;
                    for (int k = 0; k < dgv2.Rows.Count; k++)
                    {
                        for (int j = 0; j < dgv2.Columns.Count; j++)
                        {
                            if (dgv2.Rows[k].Cells[j].Value.Equals(tmpdt2.Rows[i]["流转卡号"]))
                            {
                                dgv2.Rows[k].Cells[j].ToolTipText = tmpdt2.Rows[i]["工单号"].ToString() + "\t" + tmpdt2.Rows[i]["开卡人"].ToString();
                                if (tmpdt2.Rows[i]["审核否"].Equals("Y"))
                                {
                                    dgv2.Rows[k].Cells[j].Style.BackColor = Color.Red;
                                }
                                else
                                {
                                    dgv2.Rows[k].Cells[j].Style.BackColor = Color.Yellow;
                                }
                                bSNExists = true;
                                break;
                            }
                        }
                        if (bSNExists)
                        {
                            break;
                        }
                    }
                }
            }
        }

       private void dgv2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!dgv2.CurrentCell.ToolTipText.Equals(string.Empty))
            {
                Clipboard.SetText(GetString(dgv2.CurrentCell.Value));
            }
        }

        private string GetString(object tmpobj)
        {
            try
            {
                return tmpobj.ToString().Trim();
            }
            catch
            {
                return string.Empty;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Query2();
        }

        private void iniComboBox()
        {
            cmbYear.Items.Clear();
            cmbMonth.Items.Clear();
            string strDate = sqt.DoCmdScalarData("SELECT CONVERT(varchar(100), GETDATE(), 112)").ToString();    //yyyyMMdd
            int iYear = Convert.ToInt32(strDate.Substring(0, 4));
            int iMonth = Convert.ToInt32(strDate.Substring(4, 2));
            int iDate = Convert.ToInt32(strDate.Substring(6, 2));
            if (iDate >= 24)
            {
                iMonth++;
                if (iMonth > 12)
                {
                    iMonth = 1;
                    iYear++;
                }
                iDate = 1;
            }
            //年
            cmbYear.Items.Add(iYear - 5);
            cmbYear.Items.Add(iYear - 4);
            cmbYear.Items.Add(iYear - 3);
            cmbYear.Items.Add(iYear - 2);
            cmbYear.Items.Add(iYear - 1);
            cmbYear.Items.Add(iYear);
            cmbYear.Items.Add(iYear+1);
            cmbYear.SelectedIndex = 5;
            //月
            for (int i = 1; i <= 12; i++)
            {
                cmbMonth.Items.Add(i.ToString ("00"));
            }
            cmbMonth .SelectedIndex =iMonth -1;
        }

        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Query2();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            Query3();
        }
    }
}
